//
//  YYT_PremiereViewController.m
//  AudioVisualFeast
//
//  Created by dllo on 16/1/26.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "QL_PremiereViewController.h"
#import "QL_PremiereShowView.h"
#import "QL_NavigationView.h"
#import <MJRefresh.h>
#import "QL_AFNTools.h"
#import "QL_ModleShared.h"
#import "QL_MVPlayerViewController.h"



@interface QL_PremiereViewController ()<cellClick,PremiereViewDelegate>
@property (nonatomic, retain) QL_NavigationView *daohang;
// MV推荐的数组
@property (nonatomic, retain) NSMutableArray *mvPremieredataSource;
// 网络请求拼得参数
@property (nonatomic, assign) NSInteger num;
@property (nonatomic, retain) NSString *area;

@property (nonatomic, retain)QL_PremiereShowView *showView;
@end

@implementation QL_PremiereViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
//    self.navigationController.navigationBar.translucent = NO;
    // Do any additional setup after loading the view.
    self.showView = [[QL_PremiereShowView alloc]initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT - 64 - 49)];
    self.showView.PremiereDelegate = self;
    [self.view addSubview:self.showView];
    
    self.daohang = [[QL_NavigationView alloc]initWithFrame:CGRectMake(0, 64, WIDTH, 44)];
    _daohang.delegate = self;
    self.daohang.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.daohang];
    
    // 初始化 mv页数组
    self.mvPremieredataSource = [NSMutableArray arrayWithCapacity:0];
    // 网络请求
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // ALL 全部页面
        self.area = @"ALL";
        [self getData:self.num withArea:self.area];
    });
    
    
    [self addfooter];
    [self addheader];
    
}

#pragma mark -- 网络请求
-(void)getData:(NSInteger)dataNum withArea:(NSString *)area{
    [QL_AFNTools getWithUrl:@"http://mapi.yinyuetai.com/video/list.json?D-A=0" parameter:@{@"promoTitle":@"true", @"area":area, @"offset":[NSString stringWithFormat:@"%ld", dataNum], @"size":@"20", @"supportBanner":@"true"} httpHeader:@{@"App-Id":@"10201029",@"Device-Id":@"5fc617ae1220d0969b23eaab4c8f02b5",@"Device-V":@"QW5kcm9pZF80LjQuNF8xMDgwKjE5MjBfMTEwMDA5MDAw"} resposeType:ResponseTypeJSON success:^(id result) {
        NSDictionary *dictionary = [[NSDictionary alloc] initWithDictionary:result];
        NSArray *array = dictionary[@"videos"];
        for (NSDictionary *dic in array) {
            QL_ModleShared *model = [[QL_ModleShared alloc] initWithDictionary:dic];
            [self.mvPremieredataSource addObject:model];
        }
        //  刷新tableView
        [_showView.tableview reloadData];
        
        self.showView.array = self.mvPremieredataSource;
    } fail:^(NSError *error) {
        NSLog(@"%@", error);
    }];
}

#pragma mark -- 上拉刷新
-(void)addfooter {
    self.showView.tableview.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        // 进入刷新状态后会自动调用这个block
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.num += 20;
            [self getData:self.num withArea:self.area];
            [self.showView.tableview.mj_footer endRefreshing];
        });
    }];
    
}
#pragma mark -- 下拉刷新
-(void)addheader {
    self.showView.tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.num = 0;
            [self.mvPremieredataSource removeAllObjects];
            [self getData:self.num withArea:self.area];
            [self.showView.tableview.mj_header endRefreshing];
        });
    }];
    [self.showView.tableview.mj_header beginRefreshing];
}

#pragma mark -- cellClick协议

-(void)cellClick:(NSInteger)item {
    //   点击不同的导航栏按钮 请求不同的数据
    //   全部 all
    //   日本 JP
    //   韩国 KR
    //   欧美 US
    //   港台 HT
    //   内地 ML
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (item == 5 || item == 11 || item == 17) {
            self.area = @"JP";
            [self.mvPremieredataSource removeAllObjects];
            [self getData:self.num withArea:self.area];
        } else if (item == 0 || item == 6 || item == 12) {
            // 当 area = ALL 时候 移除数组数据 重新网络加载
            self.area = @"ALL";
            [self.mvPremieredataSource removeAllObjects];
            [self getData:self.num withArea:self.area];
        } else if (item == 1 || item == 7 || item == 13) {
            self.area = @"ML";
            [self.mvPremieredataSource removeAllObjects];
            [self getData:self.num withArea:self.area];
        } else if (item == 2 || item == 8 || item == 14) {
            self.area = @"HT";
            [self.mvPremieredataSource removeAllObjects];
            [self getData:self.num withArea:self.area];
        } else if (item == 3 || item == 9 || item == 15) {
            self.area = @"US";
            [self.mvPremieredataSource removeAllObjects];
            [self getData:self.num withArea:self.area];
        } else if (item == 4 || item == 10 || item == 16) {
            self.area = @"KR";
            [self.mvPremieredataSource removeAllObjects];
            [self getData:self.num withArea:self.area];
        }
        [_showView.tableview setContentOffset:CGPointMake(0, 0)];
    });
    
}

- (void)pushToPremiereDetailVCtableview:(UITableView *)tableview didSelectIndexPath:(NSIndexPath *)indexPath{
    QL_MVPlayerViewController *mvPlayerVC = [[QL_MVPlayerViewController alloc]init];
    mvPlayerVC.playurl = [self.mvPremieredataSource[indexPath.row]urls]; // 传一个每个cell 相对应的网址、
    mvPlayerVC.headtitle = [self.mvPremieredataSource[indexPath.row] title];// 传一个标题
    mvPlayerVC.ids = [NSString stringWithFormat:@"%@", [self.mvPremieredataSource[indexPath.row] ids]]; //id
    mvPlayerVC.pic = [NSString stringWithFormat:@"%@",[self.mvPremieredataSource[indexPath.row] playListPic]]; // 图片
    [self.navigationController pushViewController:mvPlayerVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
